package com.xyy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xyy.entity.Patient;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 就诊人Mapper接口
 */
@Mapper
public interface PatientMapper extends BaseMapper<Patient> {
    
    /**
     * 统计全部健康档案总数
     * @param doctorId 医生ID（此参数保留以兼容现有接口，但实际不使用）
     * @return 健康档案总数
     */
    @Select("SELECT COUNT(*) FROM patient")
    Integer countHealthRecordsByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 统计性别分布
     */
    @Select("SELECT " +
            "CASE gender " +
            "WHEN 1 THEN '男' " +
            "WHEN 2 THEN '女' " +
            "WHEN 3 THEN '其他' " +
            "ELSE '未知' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY gender")
    List<Map<String, Object>> getGenderDistribution();
    
    /**
     * 统计年龄分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN age < 18 THEN '未成年' " +
            "WHEN age BETWEEN 18 AND 30 THEN '18-30岁' " +
            "WHEN age BETWEEN 31 AND 45 THEN '31-45岁' " +
            "WHEN age BETWEEN 46 AND 60 THEN '46-60岁' " +
            "WHEN age > 60 THEN '60岁以上' " +
            "END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN age < 18 THEN '未成年' " +
            "WHEN age BETWEEN 18 AND 30 THEN '18-30岁' " +
            "WHEN age BETWEEN 31 AND 45 THEN '31-45岁' " +
            "WHEN age BETWEEN 46 AND 60 THEN '46-60岁' " +
            "WHEN age > 60 THEN '60岁以上' " +
            "END")
    List<Map<String, Object>> getAgeDistribution();
    
    /**
     * 统计身高分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN height = '未测量' THEN '未测量' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) < 150 THEN '150cm以下' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 150 AND 160 THEN '150-160cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 161 AND 170 THEN '161-170cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 171 AND 180 THEN '171-180cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) > 180 THEN '180cm以上' " +
            "ELSE '数据异常' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN height = '未测量' THEN '未测量' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) < 150 THEN '150cm以下' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 150 AND 160 THEN '150-160cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 161 AND 170 THEN '161-170cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) BETWEEN 171 AND 180 THEN '171-180cm' " +
            "WHEN CAST(REPLACE(height, 'cm', '') AS UNSIGNED) > 180 THEN '180cm以上' " +
            "ELSE '数据异常' END")
    List<Map<String, Object>> getHeightDistribution();
    
    /**
     * 统计体重分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN weight = '未测量' THEN '未测量' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) < 50 THEN '50kg以下' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 50 AND 60 THEN '50-60kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 61 AND 70 THEN '61-70kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 71 AND 80 THEN '71-80kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) > 80 THEN '80kg以上' " +
            "ELSE '数据异常' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN weight = '未测量' THEN '未测量' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) < 50 THEN '50kg以下' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 50 AND 60 THEN '50-60kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 61 AND 70 THEN '61-70kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) BETWEEN 71 AND 80 THEN '71-80kg' " +
            "WHEN CAST(REPLACE(weight, 'kg', '') AS UNSIGNED) > 80 THEN '80kg以上' " +
            "ELSE '数据异常' END")
    List<Map<String, Object>> getWeightDistribution();
    
    /**
     * 统计血压分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN blood_pressure = '未测量' THEN '未测量' " +
            "WHEN blood_pressure LIKE '%正常%' THEN '正常' " +
            "WHEN blood_pressure LIKE '%偏高%' OR blood_pressure LIKE '%高血压%' THEN '偏高' " +
            "WHEN blood_pressure LIKE '%偏低%' OR blood_pressure LIKE '%低血压%' THEN '偏低' " +
            "ELSE '其他' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN blood_pressure = '未测量' THEN '未测量' " +
            "WHEN blood_pressure LIKE '%正常%' THEN '正常' " +
            "WHEN blood_pressure LIKE '%偏高%' OR blood_pressure LIKE '%高血压%' THEN '偏高' " +
            "WHEN blood_pressure LIKE '%偏低%' OR blood_pressure LIKE '%低血压%' THEN '偏低' " +
            "ELSE '其他' END")
    List<Map<String, Object>> getBloodPressureDistribution();
    
    /**
     * 统计血糖分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN blood_sugar = '未测量' THEN '未测量' " +
            "WHEN blood_sugar LIKE '%正常%' THEN '正常' " +
            "WHEN blood_sugar LIKE '%偏高%' OR blood_sugar LIKE '%糖尿病%' THEN '偏高' " +
            "WHEN blood_sugar LIKE '%偏低%' THEN '偏低' " +
            "ELSE '其他' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN blood_sugar = '未测量' THEN '未测量' " +
            "WHEN blood_sugar LIKE '%正常%' THEN '正常' " +
            "WHEN blood_sugar LIKE '%偏高%' OR blood_sugar LIKE '%糖尿病%' THEN '偏高' " +
            "WHEN blood_sugar LIKE '%偏低%' THEN '偏低' " +
            "ELSE '其他' END")
    List<Map<String, Object>> getBloodSugarDistribution();
    
    /**
     * 统计血氧分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN blood_oxygen = '未测量' THEN '未测量' " +
            "WHEN blood_oxygen LIKE '%正常%' THEN '正常' " +
            "WHEN blood_oxygen LIKE '%偏低%' THEN '偏低' " +
            "ELSE '其他' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN blood_oxygen = '未测量' THEN '未测量' " +
            "WHEN blood_oxygen LIKE '%正常%' THEN '正常' " +
            "WHEN blood_oxygen LIKE '%偏低%' THEN '偏低' " +
            "ELSE '其他' END")
    List<Map<String, Object>> getBloodOxygenDistribution();
    
    /**
     * 统计视力分布
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN vision = '未测量' THEN '未测量' " +
            "WHEN vision LIKE '%正常%' THEN '正常' " +
            "WHEN vision LIKE '%近视%' THEN '近视' " +
            "WHEN vision LIKE '%远视%' THEN '远视' " +
            "WHEN vision LIKE '%散光%' THEN '散光' " +
            "ELSE '其他' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN vision = '未测量' THEN '未测量' " +
            "WHEN vision LIKE '%正常%' THEN '正常' " +
            "WHEN vision LIKE '%近视%' THEN '近视' " +
            "WHEN vision LIKE '%远视%' THEN '远视' " +
            "WHEN vision LIKE '%散光%' THEN '散光' " +
            "ELSE '其他' END")
    List<Map<String, Object>> getVisionDistribution();
    
    /**
     * 统计过敏史
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN allergic_history = '无' THEN '无过敏' " +
            "WHEN allergic_history LIKE '%青霉素%' THEN '青霉素过敏' " +
            "WHEN allergic_history LIKE '%海鲜%' THEN '海鲜过敏' " +
            "WHEN allergic_history LIKE '%花粉%' THEN '花粉过敏' " +
            "ELSE '其他过敏' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN allergic_history = '无' THEN '无过敏' " +
            "WHEN allergic_history LIKE '%青霉素%' THEN '青霉素过敏' " +
            "WHEN allergic_history LIKE '%海鲜%' THEN '海鲜过敏' " +
            "WHEN allergic_history LIKE '%花粉%' THEN '花粉过敏' " +
            "ELSE '其他过敏' END")
    List<Map<String, Object>> getAllergyStatistics();
    
    /**
     * 统计既往病史
     */
    @Select("SELECT " +
            "CASE " +
            "WHEN medical_history = '无' THEN '无病史' " +
            "WHEN medical_history LIKE '%高血压%' THEN '高血压' " +
            "WHEN medical_history LIKE '%糖尿病%' THEN '糖尿病' " +
            "WHEN medical_history LIKE '%心脏病%' THEN '心脏病' " +
            "ELSE '其他疾病' END as name, " +
            "COUNT(*) as value " +
            "FROM patient " +
            "GROUP BY " +
            "CASE " +
            "WHEN medical_history = '无' THEN '无病史' " +
            "WHEN medical_history LIKE '%高血压%' THEN '高血压' " +
            "WHEN medical_history LIKE '%糖尿病%' THEN '糖尿病' " +
            "WHEN medical_history LIKE '%心脏病%' THEN '心脏病' " +
            "ELSE '其他疾病' END")
    List<Map<String, Object>> getMedicalHistoryStatistics();
    
    /**
     * 统计总就诊人数
     */
    @Select("SELECT COUNT(*) FROM patient")
    Long getTotalPatients();
    
    /**
     * 统计已测量数据的就诊人数
     */
    @Select("SELECT COUNT(*) FROM patient WHERE height != '未测量' OR weight != '未测量' OR blood_pressure != '未测量' OR blood_sugar != '未测量' OR blood_oxygen != '未测量' OR vision != '未测量'")
    Long getMeasuredPatients();
    
    /**
     * 统计数据完整性
     */
    @Select("SELECT " +
            "'身高' as name, COUNT(*) as value FROM patient WHERE height != '未测量' " +
            "UNION ALL " +
            "SELECT '体重', COUNT(*) FROM patient WHERE weight != '未测量' " +
            "UNION ALL " +
            "SELECT '血压', COUNT(*) FROM patient WHERE blood_pressure != '未测量' " +
            "UNION ALL " +
            "SELECT '血糖', COUNT(*) FROM patient WHERE blood_sugar != '未测量' " +
            "UNION ALL " +
            "SELECT '血氧', COUNT(*) FROM patient WHERE blood_oxygen != '未测量' " +
            "UNION ALL " +
            "SELECT '视力', COUNT(*) FROM patient WHERE vision != '未测量'")
    List<Map<String, Object>> getDataCompleteness();
} 